package com.fengwk.support.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.fengwk.support.exception.BaseException;
import com.fengwk.support.util.LoggerUtils.Logger;

/**
 * 对象工具
 */
public class ObjectUtils {

	private static final Logger LOG = LoggerUtils.getLogger(ObjectUtils.class);
	
	private ObjectUtils() {
		throw new BaseException("can not instance " + getClass());
	}

	/**
	 * 对象比较
	 * 
     * <pre>
     * ObjectUtils.equals(null, null)                  = true
     * ObjectUtils.equals(null, "")                    = false
     * ObjectUtils.equals("", null)                    = false
     * ObjectUtils.equals("", "")                      = true
     * ObjectUtils.equals(Boolean.TRUE, null)          = false
     * ObjectUtils.equals(Boolean.TRUE, "true")        = false
     * ObjectUtils.equals(Boolean.TRUE, Boolean.TRUE)  = true
     * ObjectUtils.equals(Boolean.TRUE, Boolean.FALSE) = false
     * </pre>
     * 
	 * @param object1
	 * @param object2
	 * @return
	 */
	public static <O> boolean equals(O o1, O o2) {
		if (o1 == o2)
			return true;
        if (o1 == null || o2 == null)
        	return false;
        return o1.equals(o2);
	}
	
	/**
	 * 构建简单对象并赋值
	 * 
	 * @param calssQualifiedName 类的全名
	 * @param val
	 * @return
	 */
	public static Object build(String calssQualifiedName, String val) {
		try {
			return build(Class.forName(calssQualifiedName), val);
		} catch (ClassNotFoundException e) {
		    LOG.error(e);
		}
		return null;
	}
	
	/**
	 * 该方法并不一定有效
	 * 
	 * @param clazz
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Deprecated
	public static <T> T build(Class<T> clazz) {
		if(clazz == List.class) {
			return (T) new ArrayList();
		}
		if(clazz == Set.class) {
			return (T) new HashSet();
		}
		if(clazz == Map.class) {
			return (T) new HashMap();
		}
		try {
			//采用无参构造
			return clazz.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
		    LOG.error(e);
		}
		return null;
	}
	
	/**
	 * 构建简单对象并赋值
	 * 
	 * @param clazz 对象字节码
	 * @param val 对象字符串值
	 * @return
	 */
	public static <T> T build(Class<T> clazz, String val) {
		if (StringUtils.isBlank(val)) {
			val = null;
		} else {
			val = "\"" + val.replace("\"", "\\\"") + "\"";
		}
		return JsonUtils.fromJson(val, clazz);
	}
	
	/**
	 * 通过全限名构建对象(采用无参构造)
	 * 
	 * @param qualifiedName
	 * @return
	 */
	public static Object build(String qualifiedName) {
		try {
			return build(Class.forName(qualifiedName));
		} catch (ClassNotFoundException e) {
		    LOG.error(e);
		}
		return null;
	}
	
	/**
	 * 序列化方法克隆
	 * 
	 * @param o
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <O extends Serializable> O clone(O o) {
		if (o == null) 
			return null;
		return (O) IoUtils.bytesToObject(IoUtils.objectToBytes(o));
	}
	
	/**
	 * json方式克隆List
	 * 
	 * @param list
	 * @param generic
	 * @return
	 */
	public static <E> List<E> clone(List<E> list, Class<E> generic) {
		if (list == null)
			return null;
		if (list.size() == 0)
			return new ArrayList<>();
		return JsonUtils.fromJsonList(JsonUtils.toJson(list), generic);
	}
	
	/**
	 * json方式克隆Set
	 * 
	 * @param set
	 * @param generic
	 * @return
	 */
	public static <E> Set<E> clone(Set<E> set, Class<E> generic) {
		if (set == null) 
			return null;
		return JsonUtils.fromJsonSet(JsonUtils.toJson(set), generic);
	}
	
}
